﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrameWork.Setting
{
    /// <summary>
    /// 为设定页面提供存储
    /// </summary>
    public static class SettingPool
    {
        private static List<SettingEditorItem> Editors = new List<SettingEditorItem>();
        private static SettingOperatorBase Operator;

        /// <summary>
        /// 初始化设定池
        /// </summary>
        /// <param name="SettingOperator">设定文件操作器</param>
        public static void Init(SettingOperatorBase SettingOperator)
        {
            Operator = SettingOperator;
        }

        /// <summary>
        /// 注册编辑器
        /// </summary>
        /// <returns></returns>
        public static bool RegisterEditor(SettingEditorItem EditorItem)
        {
            foreach (SettingEditorItem item in Editors.ToArray())
            {
                if (item.GetShowName() == EditorItem.GetShowName())
                {
                    return false;
                }
            }
            Editors.Add(EditorItem);
            return true;
        }
        
        /// <summary>
        /// 注册编辑器
        /// </summary>
        /// <param name="EditorItems">要注册的编辑器集合</param>
        /// <returns></returns>
        public static bool RegisterEditor(IEnumerable<SettingEditorItem> EditorItems)
        {
            bool IsSuc = true;
            foreach(SettingEditorItem item in EditorItems)
            {
                IsSuc &= RegisterEditor(item);
            }
            return IsSuc;
        }

        /// <summary>
        /// 获取所有设定编辑器
        /// </summary>
        /// <returns>一个数组，包含了所有已注册的数据编辑器</returns>
        public static SettingEditorItem[] GetEditorItems()
        {
            return Editors.ToArray();
        }
        /// <summary>
        /// 获取指定设定项的设定编辑器
        /// </summary>
        /// <param name="item">要获取设定编辑器的设定项</param>
        /// <returns>获取到的设定编辑器</returns>
        public static SettingEditorBase GetEditor(SettingEditorItem item)
        {
            item.IsChanged = true;
            return item.GetEditor();
        }
        /// <summary>
        /// 载入设定
        /// </summary>
        /// <param name="EditorItem">要载入设定的设定项,为Null时将载入所有的设定项</param>
        public static void LoadSetting(SettingEditorItem EditorItem = null)
        {
            if (EditorItem != null)
            {
                if (EditorItem.IsLoaded == false)
                {
                    EditorItem.LoadSetting(Operator);
                    EditorItem.IsLoaded = true;
                    EditorItem.IsChanged = false;
                }
                return;
            }
            foreach (SettingEditorItem item in Editors)
            {
                if (!item.IsLoaded)
                {
                    item.LoadSetting(Operator);
                    item.IsLoaded = true;
                    item.IsChanged = false;
                }
            }
        }
        /// <summary>
        /// 应用设定
        /// </summary>
        public static void AcceptSetting(SettingEditorItem EditorItem = null)
        {
            if (EditorItem != null)
            {
                if (EditorItem.IsLoaded && EditorItem.IsChanged)
                {
                    EditorItem.AcceptSetting();
                    EditorItem.IsChanged = true;
                }
                return;
            }
            foreach (SettingEditorItem item in Editors)
            {
                if (item.IsLoaded && item.IsChanged)
                {
                    item.AcceptSetting();
                    item.IsChanged = true;
                }
            }
        }
        /// <summary>
        /// 重置编辑器
        /// </summary>
        /// <param name="EditorItem"></param>
        public static void ResetEditor(SettingEditorItem EditorItem = null)
        {
            if (EditorItem != null)
            {
                if (EditorItem.IsLoaded)
                {
                    EditorItem.ResetEditor();
                    EditorItem.IsChanged = false;
                }
                return;
            }
            foreach (SettingEditorItem item in Editors)
            {
                if (item.IsLoaded)
                {
                    item.ResetEditor();
                    item.IsChanged = false;
                }
            }
        }
        /// <summary>
        /// 保存设定
        /// </summary>
        /// <param name="EditorItem">要保存的设定项，为Null时将保存所有设定项</param>
        public static void SaveSetting(SettingEditorItem EditorItem = null)
        {
            if (EditorItem != null)
            {
                if (EditorItem.IsChanged && EditorItem.IsLoaded)
                {
                    EditorItem.SaveSetting(Operator);
                    EditorItem.IsChanged = false;
                }
                return;
            }
            foreach (SettingEditorItem item in Editors)
            {
                if (item.IsChanged && item.IsLoaded)
                {
                    item.SaveSetting(Operator);
                    item.IsChanged = false;
                }
            }
        }
        /// <summary>
        /// 重置设定
        /// </summary>
        /// <param name="EditorItem"></param>
        public static void ResetToDefault(SettingEditorItem EditorItem = null)
        {
            if (EditorItem != null)
            {
                EditorItem.ResetToDefault();
                EditorItem.IsLoaded = true;
                EditorItem.IsChanged = true;
                return;
            }
            foreach (SettingEditorItem item in Editors)
            {
                if (!item.IsLoaded)
                {
                    item.ResetToDefault();
                    item.IsLoaded = true;
                    EditorItem.IsChanged = true;
                }
            }
        }

    }
}
